'use strict';
let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    access_token: String,
    expires_in: Number,
    refresh_token: String,
    openid: String,
    scope: String,
    create_at: String
},{versionKey:false});

schema.statics = {
    getToken : async function (openid) {
        return await this.findOne({openid:openid});
    },
    setToken : async function (openid, token) {
        // 有则更新，无则添加
        var query = {openid: openid};
        var options = {upsert: true};
        await this.update(query, token, options);
    }
};

module.exports = schema;